import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards } from '@nestjs/common';
import { OrganizationsService } from './organizations.service';
import { CreateOrganizationDto } from './dto/create-organization.dto';
import { UpdateOrganizationDto } from './dto/update-organization.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { PermissionsGuard } from '../common/guards/permissions.guard';
import { Permissions } from '../common/decorators/permissions.decorator';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';

@ApiTags('组织管理')
@ApiBearerAuth()
@Controller('organizations')
@UseGuards(JwtAuthGuard, PermissionsGuard)
export class OrganizationsController {
  constructor(private readonly organizationsService: OrganizationsService) {}

  @Post()
  @Permissions('organization:create')
  @ApiOperation({ summary: '创建组织', description: '创建新组织机构' })
  @ApiResponse({ status: 201, description: '组织创建成功' })
  create(@Body() createOrganizationDto: CreateOrganizationDto) {
    return this.organizationsService.create(createOrganizationDto);
  }

  @Get()
  @Permissions('organization:read')
  @ApiOperation({ summary: '获取组织列表', description: '获取所有组织或指定父组织下的子组织' })
  @ApiResponse({ status: 200, description: '返回组织列表' })
  findAll(@Query('parentId') parentId?: string) {
    return this.organizationsService.findAll(parentId);
  }

  @Get('tree')
  @Permissions('organization:read')
  @ApiOperation({ summary: '获取组织树', description: '以树状结构获取组织层级' })
  @ApiResponse({ status: 200, description: '返回组织树结构' })
  findOrganizationTree(@Query('parentId') parentId?: string) {
    return this.organizationsService.findOrganizationTree(parentId);
  }

  @Get(':id')
  @Permissions('organization:read')
  @ApiOperation({ summary: '获取单个组织', description: '根据ID获取组织详情' })
  @ApiResponse({ status: 200, description: '返回组织信息' })
  findOne(@Param('id') id: string) {
    return this.organizationsService.findOne(id);
  }

  @Patch(':id')
  @Permissions('organization:update')
  @ApiOperation({ summary: '更新组织', description: '更新组织信息' })
  @ApiResponse({ status: 200, description: '组织更新成功' })
  update(@Param('id') id: string, @Body() updateOrganizationDto: UpdateOrganizationDto) {
    return this.organizationsService.update(id, updateOrganizationDto);
  }

  @Delete(':id')
  @Permissions('organization:delete')
  @ApiOperation({ summary: '删除组织', description: '根据ID删除组织' })
  @ApiResponse({ status: 200, description: '组织删除成功' })
  remove(@Param('id') id: string) {
    return this.organizationsService.remove(id);
  }
  
  @Post('batch-delete')
  @Permissions('organization:delete')
  @ApiOperation({ summary: '批量删除组织', description: '批量删除多个组织' })
  @ApiResponse({ status: 200, description: '组织批量删除成功' })
  removeMany(@Body('ids') ids: string[]) {
    return this.organizationsService.removeMany(ids);
  }
} 